<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2015-09-17
 * 版    本：1.0.0
 * 功能说明：模块公共文件。
 *
 **/


function UpImage($callBack = "image", $width = 100, $height = 100, $image = "")
{

    echo '<iframe scrolling="no" frameborder="0" border="0" onload="this.height=this.contentWindow.document.body.scrollHeight;this.width=this.contentWindow.document.body.scrollWidth;" width=' . $width . ' height="' . $height . '"  src="' . U('Upload/uploadpic',
            array('Width' => $width, 'Height' => $height, 'BackCall' => $callBack)) . '"></iframe>
         <input type="hidden" ' . 'value = "' . $image . '"' . 'name="' . $callBack . '" id="' . $callBack . '">';
}

function BatchImage($callBack = "image", $width = 100, $height = 100, $image = "")
{
    
    echo '<iframe scrolling="no" frameborder="0" border="0" width=100% onload="this.height=this.contentWindow.document.body.scrollHeight;" src="' . U('Upload/batchpic',
            array('Width' => $width, 'Height' => $height, 'BackCall' => $callBack)) . '"></iframe>
		<input type="hidden" ' . 'value = "' . $image . '"' . 'name="' . $callBack . '" id="' . $callBack . '">';
}


/*
 * 函数：网站配置获取函数
 * @param  string $k      可选，配置名称
 * @return array          用户数据
*/
function setting($k = 'all')
{
    $cache = S($k);
    //如果缓存不为空直接返回
    if (null != $cache) {
        return $cache;
    }
    $data = '';
    $setting = M('setting');
    //判断是否查询全部设置项
    if ($k == 'all') {
        $setting = $setting->field('k,v')->select();
        foreach ($setting as $v) {
            $config[$v['k']] = $v['v'];
        }
        $data = $config;

    } else {
        $result = $setting->where("k='{$k}'")->find();
        $data = $result['v'];

    }
    //建立缓存
    if ($data) {
        S($k, $data);
    }
    return $data;
}

/**
 * 函数：格式化字节大小
 * @param  number $size 字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '')
{
    $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    for ($i = 0; $size >= 1024 && $i < 5; $i++) {
        $size /= 1024;
    }
    return round($size, 2) . $delimiter . $units[$i];
}

/**
 * 函数：加密
 * @param string            密码
 * @return string           加密后的密码
 */
function password($password)
{
    /*
    *后续整强有力的加密函数
    */
    return md5('Q' . $password . 'W');

}

/**
 * 随机字符
 * @param number $length 长度
 * @param string $type 类型
 * @param number $convert 转换大小写
 * @return string
 */
function random($length = 6, $type = 'string', $convert = 0)
{
    $config = array(
        'number' => '1234567890',
        'letter' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
        'string' => 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
        'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
    );

    if (!isset($config[$type])) {
        $type = 'string';
    }
    $string = $config[$type];

    $code = '';
    $strlen = strlen($string) - 1;
    for ($i = 0; $i < $length; $i++) {
        $code .= $string{mt_rand(0, $strlen)};
    }
    if (!empty($convert)) {
        $code = ($convert > 0) ? strtoupper($code) : strtolower($code);
    }
    return $code;
}

//获取所有的子级id
function category_get_sons($sid, &$array = array())
{
    //获取当前sid下的所有子栏目的id
    $categorys = M("category")->where("pid = {$sid}")->select();

    $array = array_merge($array, array($sid));
    foreach ($categorys as $category) {
        category_get_sons($category['id'], $array);
    }
    $data = $array;
    unset($array);
    return $data;

}


/**
 * 获取文章url地址
 * url结构：ttp://wwww.qwadmin.com/分类/子分类/子分类/id.html
 * 使用方法：模板中{:articleUrl(array('aid'=>$val['aid']))}
 *
 *
 * @param $data
 * @return $string
 */
function articleUrl($data)
{
    //如果数组为空直接返回空字符
    if (!$data) {
        return '';
    }
    //如果参数错误直接返回空字符
    if (!isset($data['aid'])) {
        return '';
    }

    $aid = (int)$data['aid'];

    //获取文章信息
    $article = M('article')->where(array('aid' => $aid))->find();
    //获取当前内容所在分类
    $category = M('category')->where(array('id' => $article['sid']))->find();
    //获取当前分类
    $categoryUrl = $category['dir'];
    //遍历获取当前文章所在分类的有上级分类并且组合url
    while ($category['pid'] <> 0) {
        $category = M('category')->where(array('id' => $category['pid']))->find();
        $categoryUrl = $category['dir'] . "/" . $categoryUrl;
        //如果上级分类已经无上级分类则退出
    }

    $categoryUrl = __ROOT__ . "/" . $categoryUrl;
    //组合文章url
    $articleUrl = $categoryUrl . '/' . $aid . ".html";
    return $articleUrl;

}






/**
 *
 * excel列表下载
 * @param array $listArray 列表数据
 * @param array $headerTitleArr 列名数组
 * @param array $contentArr 列表内容数组
 * @param string $outFileName excel文件名
 *
 * @author cobray.bao
 * @date 2015-02-27
 */
function downloadExcel($listArray,$headerTitleArr,$contentArr,$outFileName){
    if(!class_exists('PHPExcel')){
        vendor("PHPExcel.PHPExcel");
    }
    $objPHPExcel = new PHPExcel();
    $sharedStyle = new PHPExcel_Style();
    //设置表头
    foreach($headerTitleArr as $key => $hItem){
        $i = PHPExcel_Cell::stringFromColumnIndex($key).'1';
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($i,$hItem);
        $maxFromColumnIndex = $i;
    }

    //设置颜色
    $sharedStyle->applyFromArray(
        array('fill'    => array(
            'type'      => PHPExcel_Style_Fill::FILL_SOLID,
            'color'     => array('argb' => 'FFFFFF')
        ),
            'borders' => array(
                'bottom'    => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                'right'     => array('style' => PHPExcel_Style_Border::BORDER_MEDIUM)
            )
        ));
    $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle, "A1:".$maxFromColumnIndex);

    //向表格填充数据
    $_index = 2;
    if($listArray){
        foreach($listArray as $key => $item){
            foreach($contentArr AS $k => $v){
                if (strlen($item[$v])>15 && is_numeric($item[$v]))
                {
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue(PHPExcel_Cell::stringFromColumnIndex($k).$_index, "'$item[$v]'");
                }
                else
                {
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue(PHPExcel_Cell::stringFromColumnIndex($k).$_index, $item[$v]);
                }
            }
            $_index++;
        }
    }
    $objPHPExcel->setActiveSheetIndex(0);
    $outFileName = iconv("utf-8", "gb2312", $outFileName);
    //输出
    header('Content-Type: application/vnd.ms-excel;charset=utf-8');
    header('Content-Disposition: attachment;filename='.$outFileName.'');
    header('Cache-Control: max-age=0');
    header('Cache-Control: max-age=1');
    header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
    header('Cache-Control: cache, must-revalidate');
    header('Pragma: public');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit();
}
/**
 **$excelpath 路径
 *$model 模型名称
 *$tableCols 表的字段名
 *$rest 表中其余想添加的字段
 */
function importExcel($excelpath){
    
    vendor("PHPExcel.PHPExcel");
    vendor("PHPExcel.IOFactory");
    vendor("PHPExcel.Excel5");
    $objReader = PHPExcel_IOFactory::createReader('Excel5'); //use Excel5 for 2003 format
    if (!file_exists($excelpath)) {
    die('no file!');
    }
    $extension = strtolower( pathinfo($excelpath, PATHINFO_EXTENSION) );

    if ($extension =='xlsx') {
        $objReader = new PHPExcel_Reader_Excel2007();
        $objExcel = $objReader ->load($excelpath);
    } else if ($extension =='xls') {
        $objReader = new PHPExcel_Reader_Excel5();
        $objExcel = $objReader ->load($excelpath);
    } else if ($extension=='csv') {
        $PHPReader = new PHPExcel_Reader_CSV();

        //默认输入字符集
        $PHPReader->setInputEncoding('GBK');

        //默认的分隔符
        $PHPReader->setDelimiter(',');

        //载入文件
        // $objExcel = $PHPReader->load($file);
    }

    // $sheet = $objExcel ->getSheet(0);
    
    $excelpath=$excelpath;

    $objPHPExcel = $objReader->load($excelpath);

    $sheet = $objPHPExcel->getSheet(0);

    $highestRow = $sheet->getHighestRow();           //取得总行数

    $highestColumn = $sheet->getHighestColumn(); //取得总列数
    $data=array();
    for($j=3;$j<=$highestRow;$j++)                        //从第二行开始读取数据

    {

        $str="";

        for($k='A';$k<=$highestColumn;$k++)            //从A列读取数据

        {

            $str .=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().'|*|';//读取单元格

        }

        $str=mb_convert_encoding($str,'utf8','auto');//根据自己编码修改

        $strs = explode("|*|",$str);
        $data[]=$strs;
    }
    return $data;
}


